package web2023.team.homework_10_aop.aspect;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import web2023.team.homework_10_aop.entity.Login_info;
import web2023.team.homework_10_aop.service.LoginInfoService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;

@Component
@Aspect
public class MyAspect{
    @Autowired
    LoginInfoService loginInfoService;
    @AfterReturning(value="execution(* web2023.team.homework_10_aop.service.UserServiceImpl.*(..))",
                    returning="msg")
    public void beforeMethod(Object msg) throws IOException {
        if(msg.equals(true)){
            System.out.println("后置通知..."+msg);
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
            // 获取当前用户名
            String username = request.getParameter("username");
            // 获取用户ip地址
            //String ip = request.getRemoteAddr();
            String ip = getIpAddress(request);
            // 获取当前登陆时间
            Date date = new Date();
            Timestamp currentTime = new Timestamp(date.getTime());
            boolean result =  loginInfoService.insertLoginInfo(new Login_info(null,username,ip,currentTime));
            System.out.println("用户登录日志写入成功");
        }else{
            System.out.println("用户登录日志写入失败");
        }
    }
    public final static String getIpAddress(HttpServletRequest request)
            throws IOException {
        // 获取请求主机IP地址,如果通过代理进来，则透过防火墙获取真实IP地址

        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0
                    || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    // //最终通知
    //@After("execution(* web2022.team6.huyifan.homework_10_aop.service.UserServiceImpl.*(..))")
    //public void after(){
    //    System.out.println("最终通知...");
    //}

}
